/*
 * @lc app=leetcode id=202 lang=cpp
 *
 * [202] Happy Number
 */
class Solution {
public:
    bool isHappy(int n) {
        if (n < 1) {
            return false;
        }

        int m = 0; 
        unordered_set<int> memo;
        while (true) {
            while (n) {
                m += (n%10)*(n%10);
                n = n/10;
            }

            if (m == 1) {
                return true;
            }

            if (memo.find(m) != memo.end()) {
                return false;
            }
            memo.insert(m);
            n = m;
            m = 0;
        }
        return false;
        
    }
};

